package com.ewanghuiju.app.ui.pintuan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.PintuanOnlineListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanShopDetailsActivity;
import com.ewanghuiju.app.util.NoDoubleClickListener;
import com.ewanghuiju.app.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by chenyw on 2019-08-15.
 */
public class AutoRollAdapter extends RecyclerView.Adapter<AutoRollAdapter.BaseViewHolder> {
    HashMap<String, CountDownTimer> timerMap = new HashMap<>();
    ArrayList<String> timerKeyList = new ArrayList<>();
    private List<PintuanOnlineListResponBean> mData;
    HashMap<String, Long> secondsMap = new HashMap<>();
    private Context mContext;
    private int mType;

    public AutoRollAdapter(List<PintuanOnlineListResponBean> list, Context mContext, int type) {
        this.mData = list;
        this.mContext = mContext;
        this.mType = type;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pintuan_info_viewflipper, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    public void setDataChange(List<PintuanOnlineListResponBean> list) {
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        PintuanOnlineListResponBean data = mData.get(position % mData.size());
        holder.mTvName.setText(data.getNickname());
        holder.mTvIntro.setText("你知道我在等你吗");
        ImageLoader.load(mContext, data.getUser_head_image(), holder.mIvAvator);

        if(data.getUser_yes()==1){
            holder.tvGoCantuan.setText("已参团");
            holder.tvGoCantuan.setBackgroundResource(R.drawable.corner_999_3);
            holder.tvGoCantuan.setClickable(false);
        }else{
            holder.tvGoCantuan.setText("去参团");
            holder.tvGoCantuan.setBackgroundResource(R.drawable.corner_primary_button_3);
            holder.tvGoCantuan.setClickable(true);
        }

        long currenTime = System.currentTimeMillis() / 1000;
        long endTime = data.getEnd_time();
        long seconds = endTime - currenTime;
        String index = String.valueOf(position);

        holder.mLayoutGoGourp.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                //去参团
                ((PintuanShopDetailsActivity) mContext).toCantuan(data.getId());
            }
        });
        SpannableString spannableString = new SpannableString("还差" + data.getResidue_user() + "人成团");
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFD4747")), 2, spannableString.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.mTvleaveMembers.setText(spannableString);
        String id = data.getId();
        if (!timerKeyList.contains(index)) {
            timerKeyList.add(index);
        }

        // -----一开始初始化数据

        if (holder.countDownTimer != null) {
            holder.countDownTimer.cancel();
        }


        if (secondsMap.get(id) == null) {
            holder.countDownTimer = new CountDownTimer(seconds * 1000, 1000) {
                public void onTick(long l) {
                    holder.mTvCountDown.setText(StringUtil.getTimeStr(l));
                    secondsMap.put(id, l);
                }

                public void onFinish() {
                    //倒计时结束 刷新商品信息
                    RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_SHOP_INFO));
                }
            }.start();
        } else {
            holder.countDownTimer = new CountDownTimer(secondsMap.get(id), 1000) {
                public void onTick(long l) {
                    holder.mTvCountDown.setText(StringUtil.getTimeStr(l));
                    secondsMap.put(id, l);
                }

                public void onFinish() {
                    //倒计时结束 刷新商品信息
                    RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_SHOP_INFO));
                }
            }.start();
        }
        timerMap.put(index, holder.countDownTimer);
    }

    @Override
    public int getItemCount() {
//        return mData.size() > 2 ? Integer.MAX_VALUE : mData.size();
        return (mType == 2 && mData.size() > 2) ? Integer.MAX_VALUE : mData.size();
    }


    public void onDestroy() {
        for (int i = 0; i < timerKeyList.size(); i++) {
            if (timerKeyList.get(i) != null) {
                if (timerMap.get(timerKeyList.get(i)) != null) {
                    CountDownTimer timer = timerMap.get(timerKeyList.get(i));
                    if (timer != null) {
                        timer.cancel();
                    }
                }
            }
        }
    }


    public class BaseViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvCountDown;
        private ImageView mIvAvator;
        private TextView mTvName, mTvIntro;
        private TextView mTvleaveMembers;
        private TextView mLayoutGoGourp;
        private TextView tvGoCantuan;
        private CountDownTimer countDownTimer;

        public BaseViewHolder(View view) {
            super(view);
            mIvAvator = view.findViewById(R.id.riv_head_one);
            mTvName = view.findViewById(R.id.tv_pintuan_name_one);
            mTvIntro = view.findViewById(R.id.tv_pintuan_intro_one);
            mTvleaveMembers = view.findViewById(R.id.tv_pintuan_num_one);
            mTvCountDown = view.findViewById(R.id.tv_pintuan_time_one);
            mLayoutGoGourp = view.findViewById(R.id.go_cantuan_one);
            tvGoCantuan = view.findViewById(R.id.go_cantuan_one);
        }
    }


}